package com.fragment;

import java.util.ArrayList;

import util.ApplicationConstant;

import com.activity.AddMyLocate;
import com.activity.GetDirection;
import com.activity.MainAppActivity;
import com.activity.MapActivity;
import com.activity.R;
import com.google.android.gms.internal.in;

import dao.FileMyLocate;
import entities.MyLocate;
import adapter.AdapterMyLocate;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

public class MyLocateFragment extends Fragment{
	private MainAppActivity mainAppActivity;
	public static LinearLayout notFoundLayout;
	public static ArrayList<MyLocate> listMyLocates;
	private AdapterMyLocate adapterMyLocate;
	private ListView listView;
	public static FileMyLocate fileMyLocate;
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d("--thoainguyenMyLocateFragment onresume ","");
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View root=inflater.inflate(R.layout.my_locate, container,false);
		listView=(ListView) root.findViewById(R.id.list_my_locate);
		notFoundLayout=(LinearLayout) root.findViewById(R.id.my_locate_not_found_layout);
		fileMyLocate=new FileMyLocate(getActivity());
		listMyLocates=fileMyLocate.loadAll();
		if(listMyLocates.size()==0)
			notFoundLayout.setVisibility(View.VISIBLE);
		else
			notFoundLayout.setVisibility(View.GONE);
		adapterMyLocate=new AdapterMyLocate(getActivity(), R.layout.my_locate_item, listMyLocates);
		listView.setAdapter(adapterMyLocate);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(mainAppActivity,MapActivity.class);
				intent.putExtra(ApplicationConstant.ACTION, ApplicationConstant.MARK_ON_MAP_MY_LOCATE);
				intent.putExtra(ApplicationConstant.POSTION, pos);
				startActivity(intent);
			}
		});
		registerForContextMenu(listView);
		return root;
	}
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		this.mainAppActivity=(MainAppActivity) activity;
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		mainAppActivity.getMenuInflater().inflate(R.menu.context_my_locate, menu);
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.menu_ml_delete:
		{
			fileMyLocate.delete(listMyLocates.get(info.position));
			listMyLocates.remove(info.position);
			if(listMyLocates.size()==0)
				notFoundLayout.setVisibility(View.VISIBLE);
			adapterMyLocate.notifyDataSetChanged();
		}
			break;
		case R.id.menu_ml_edit:
		{
			Intent intent=new Intent(mainAppActivity,AddMyLocate.class);
			intent.putExtra(ApplicationConstant.POSTION, info.position);
			startActivity(intent);
		}
			break;
		case R.id.menu_ml_get_direction:
		{
			Intent intent=new Intent(mainAppActivity,GetDirection.class);
			MyLocate locate=listMyLocates.get(info.position);
			intent.putExtra(ApplicationConstant.LAT2,locate.getLa());
			intent.putExtra(ApplicationConstant.LOG2,locate.getLog());
			intent.putExtra(ApplicationConstant.NAME2,locate.getName());
			GetDirection.MY_LOCATE_MARK=true;
			startActivity(intent);
		}
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}
	public void notifyAdaper(){
		listMyLocates=fileMyLocate.loadAll();
		if(listMyLocates.size()==0)
			notFoundLayout.setVisibility(View.VISIBLE);
		else
			notFoundLayout.setVisibility(View.GONE);
		adapterMyLocate=new AdapterMyLocate(getActivity(), R.layout.my_locate_item, listMyLocates);
		listView.setAdapter(adapterMyLocate);
	}
}
